<?php

namespace App\Jobs;

use App\Modules\Home\Models\FoodsDetail;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use DB;
use Illuminate\Support\Facades\Log;

class AddFoods implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $data;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($param)
    {
        //
        $this->data = $param;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        foreach ($this->data as $key => $data){
            $origin_id = $data['id'];
            $foods_detail = FoodsDetail::where('origin_id', $origin_id)->first();
            if(empty($foods_detail)) {
                $data_to_insert = array(
                    'origin_id'=>$origin_id,
                    'name'=>$data['name'],
                    'cat_id'=>0,
                    'img'=>$data['thumb_image_url'],
                    'code'=>$data['code'],
                    'info'=>json_encode($data),
                );
                FoodsDetail::create($data_to_insert);
            }
        }
    }
}
